package com.cloud.business.config;

import com.cloud.core.common.HttpContentResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 应用模块名称
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author Rex
 * @since 2020/3/3 15:08
 */
@RestControllerAdvice
public class ExceptionHandlerAdvice {

    /**
     * 抛出参数不合法异常
     *
     * @param exception
     * @return
     * @author Rex
     */
    @ExceptionHandler({IllegalArgumentException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public HttpContentResult badRequestException(IllegalArgumentException exception) {
        return HttpContentResult.errorResult(exception.getMessage());
    }

    /**
     * 抛出运行时异常
     *
     * @param exception
     * @return
     * @author Rex
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public HttpContentResult handler(RuntimeException exception) {
        return HttpContentResult.errorResult(exception.getMessage());
    }
}
